package pure.engine.model 
{
	import flash.geom.Rectangle;
	import pure.engine.core.VisibleCore;
	import pure.engine.world.Mirror;
	import pure.engine.core.ns_despair;
	use namespace ns_despair;
	
	/**
	 * 渲染器携带者
	 * [Author] - PURE
	 * 
	 */
public class RendererCarrier extends VisibleCore
{
	
	

	//======================
	// Property
	//======================
	

	/**
	 * 首渲染器
	 */
	public function get headRenderer():IRenderer 
	{ 
		return m_rendererList[0]; 
	}
	
	
	/**
	 * 尾渲染器
	 */
	public function get tailRenderer():IRenderer
	{
		return m_rendererList[m_numRenderer - 1];
	}

	
	

	/**
	 * 生成动画渲染器
	 * 
	 * [Param] - section  片段名字
	 * [Param] - name     渲染器名字
	 */
	public function createAnimeRenderer(section:String, name:String = null):IAnime
	{
		var R:AnimeRenderer = new AnimeRenderer();
		
		R.setSection(section);
		_addRenderer(R, name);
		return R;
	}
	
	
	/**
	 * 生成文本渲染器
	 * 
	 * [Param] text
	 * [Param] offsetX
	 * [Param] offsetY
	 * [Param] gapX
	 * [Param] gapY
	 * [Param] name
	 */
	public function createTextRenderer(text:String, offsetX:Number = 0, offsetY:Number = 0, gapX:Number = 0, gapY:Number = 0, name:String = null):IText
	{
		var R:TextRenderer = new TextRenderer(text, offsetX, offsetY, gapX, gapY);
		
		_addRenderer(R, name);
		return R;
	}
	
	
	/**
	 * 生成图形渲染器
	 * 
	 * [Param] offsetX
	 * [Param] offsetY
	 * [Param] name
	 */
	public function createShapeRenderer(offsetX:Number = 0, offsetY:Number = 0, name:String = null):IShape
	{
		var R:ShapeRenderer = new ShapeRenderer(offsetX, offsetY);
		
		_addRenderer(R, name);
		return R;
	}
	

	/**
	 * 获取渲染器
	 * 
	 * [Param] name
	 */
	public function getRenderer(name:String):IRenderer 
	{
		for (var i:int = 0; i < m_numRenderer; i++) 
		{
			if (m_rendererList[i].m_name == name) 
			{
				return m_rendererList[i];
			}
		}
		return null;
	}
	
	
	/**
	 * 索引获取渲染器
	 */
	public function getRendererAt(index:int):IRenderer
	{
		return m_rendererList[index];
	}
	
	
	

	/* ----------------------------------------------------------------------------------------------------------------
	 * ★★★★★★★★★★★★★★★★★★★★★  private  ★★★★★★★★★★★★★★★★★★★★★
	 * ----------------------------------------------------------------------------------------------------------------
	 */

	

	
	/**
	 * 加入渲染器
	 * 
	 * [Param] - R
	 * [Param] - name
	 */
	ns_despair function _addRenderer(R:Renderer, name:String):void
	{
		R.m_name                         =  name;
		m_rendererList[m_numRenderer++]  =  R;
		R.initialize();
	}
	
	
	/**
	 * 毁灭
	 */
	override ns_despair function destroy():void
	{
		super.destroy();
		
		while (--m_numRenderer > -1)
		{
			m_rendererList[m_numRenderer].destroy();
		}
		m_rendererList.length = m_numRenderer = m_x = m_y = 0;
	}
	
	
	
	
	//======================
	// Member
	//======================
	
	
	ns_despair var m_rendererList:Vector.<Renderer> = new Vector.<Renderer>();  // 渲染器列表

	ns_despair var m_numRenderer:int;  // 渲染器数目

}

}